﻿using BootstrapBlazor.Components;
using CodeMan.Application.Contracts.ClassGens;
using CodeMan.Domain.ClassGens;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace CodeMan.Application.ClassAttributes
{
    public class ClassAttributeAppService : IClassAttributeAppService

    {
        private IClassAttributeRepository _repository;
        private IObjectMapper ObjectMapper;
        private readonly ILogger _logger;

        private ClassAttributeDTO? Model { get; set; }

        public ClassAttributeAppService(IClassAttributeRepository repository, IObjectMapper objectMapper,
            ILogger<ClassAttributeAppService> logger)
        {
            _repository = repository;
            ObjectMapper = objectMapper;
            _logger = logger;

        }

        public async Task<bool> AddAsync(ClassAttributeDTO model)
        {
            Model = model;

            var saveItem = ObjectMapper.Map<ClassAttributeDTO, ClassAttribute>(model);

            await _repository.AddAsync(saveItem);

            return true;
        }

        public async Task CancelAsync()
        {
            await _repository.CancelAsync();
            await Task.CompletedTask;
        }

        public Task<bool> DeleteAsync(IEnumerable<ClassAttributeDTO> models)
        {

            var dels = ObjectMapper.Map<IEnumerable<ClassAttributeDTO>, IEnumerable<ClassAttribute>>(models);
            return _repository.DeleteAsync(dels);
        }

        public Task EditAsync(object model)
        {
            var currdto = model as ClassAttributeDTO;
            var cur = ObjectMapper.Map<ClassAttributeDTO, ClassAttribute>(currdto);
            return _repository.EditAsync(cur);
        }

        public async Task<QueryData<ClassAttributeDTO>> QueryAsync(QueryPageOptions option)
        {

            var result = await _repository.QueryAsync(option);

            var tomaps = new List<ClassAttributeDTO>();
            foreach (ClassAttribute item in result.Items)
            {
                tomaps.Add(ObjectMapper.Map<ClassAttribute, ClassAttributeDTO>(
                    item));
            }

            // 注意：未处理搜索，此处设置 IsSearched=true 后会导致高级搜索按钮高亮
            var ret = new QueryData<ClassAttributeDTO>()
            {
                TotalCount = result.TotalCount,
                Items = tomaps,
                IsSorted = result.IsSorted,
                IsFiltered = result.IsFiltered
            };
            return ret;
        }

        public async Task<bool> SaveAsync(ClassAttributeDTO model, ItemChangedType changedType)
        {
            try
            {
                var saveItem = ObjectMapper.Map<ClassAttributeDTO, ClassAttribute>(model);
                await _repository.SaveAsync(saveItem,changedType);

                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "SaveAsync");
                return false;
            }
        }

        public async Task<bool> AddDetailAsync(object keyid, ClassAttributeDTO model)
        {
            Model = model;
            Model.ClassGenId = keyid.TryToInt64();

            var saveItem = ObjectMapper.Map<ClassAttributeDTO, ClassAttribute>(model);
            var result = await _repository.AddDetailAsync(keyid, saveItem);

            return result;
        }

        public async Task<QueryData<ClassAttributeDTO>> QueryDetailAsync(object keyid, QueryPageOptions option)
        {
            try
            {

                var result = await _repository.QueryDetailAsync(keyid, option);

                var tomaps = new List<ClassAttributeDTO>();

                if (result.TotalCount > 0)
                {
                    foreach (ClassAttribute item in result.Items)
                    {
                        tomaps.Add(ObjectMapper.Map<ClassAttribute, ClassAttributeDTO>(
                            item));
                    }
                }

                // 注意：未处理搜索，此处设置 IsSearched=true 后会导致高级搜索按钮高亮
                var ret = new QueryData<ClassAttributeDTO>()
                {
                    TotalCount = result.TotalCount,
                    Items = tomaps,
                    IsSorted = result.IsSorted,
                    IsFiltered = result.IsFiltered                     
                };
                return ret;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "QueryDetailAsync");
                return null;
            }
        } 
    }
}
